package com.yuanqunsoft.alimigration.dao.impl;

import java.util.List;

import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.hibernate.*;

import com.yuanqunsoft.alimigration.dao.IRemediationDao;
import com.yuanqunsoft.alimigration.model.Remediation;
import com.yuanqunsoft.alimigration.model.Rule;

public class RemediationDaoImpl extends HibernateDaoSupport implements IRemediationDao {

    private int currentPage;
    private int allPage;
    private int pageSize;

    @Override
    public boolean save(Remediation re) {
        // TODO Auto-generated method stub
        try {
            this.getHibernateTemplate().save(re);
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    @Override
    public boolean delete(Remediation re) {
        // TODO Auto-generated method stub
        try {
            this.getHibernateTemplate().delete(re);
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    @Override
    public boolean update(Remediation re) {
        // TODO Auto-generated method stub
        try {
            this.getHibernateTemplate().update(re);
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    @Override
    public List<Remediation> getAllRemediations() {
        // TODO Auto-generated method stub
        String hql = "from Remediation";
        return this.getHibernateTemplate().find(hql);
    }

    @Override
    public List<Remediation> queryPage(int currentPge, final int pageSize) {
        // TODO Auto-generated method stub
        return (List<Remediation>) this.getHibernateTemplate().execute(new HibernateCallback() {

            public Object doInHibernate(Session s) {

                String hql = "from Remediation";
                Query q = s.createQuery(hql);
                List<Remediation> r1 = q.list();
                allPage = (r1.size() + pageSize - 1) / pageSize;
                q = s.createQuery(hql);
                q.setMaxResults(pageSize);
                q.setFirstResult((currentPage - 1) * pageSize);
                return q.list();
            }
        });
    }

    @Override
    public int getAllPage() {
        // TODO Auto-generated method stub
        return this.allPage;
    }

    public int getCurrentPage() {
        return currentPage;
    }

    public void setCurrentPage(int currentPage) {
        this.currentPage = currentPage;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public void setAllPage(int allPage) {
        this.allPage = allPage;
    }

    @Override
    public Remediation getRemediationById(int id) {
        // TODO Auto-generated method stub
        return this.getHibernateTemplate().get(Remediation.class, id);
    }

    @Override
    public List<Rule> getRuleByRemediationId(int id) {
        // TODO Auto-generated method stub
        String hql = "from Rule r where r.remediation.remId=?";
        return this.getHibernateTemplate().find(hql, id);
    }

}
